<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
        }
        .container {
            width: 300px;
            margin: auto;
            padding: 20px;
            background-color: #fff;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            margin-top: 100px;
        }
        input[type="text"],
        input[type="password"],
        input[type="submit"] {
            width: 100%;
            padding: 10px;
            margin: 5px 0;
            display: inline-block;
            border: 1px solid #ccc;
            border-radius: 4px;
            box-sizing: border-box;
        }
        input[type="submit"] {
            background-color: #4caf50;
            color: white;
            border: none;
            cursor: pointer;
        }
        input[type="submit"]:hover {
            background-color: #45a049;
        }
    </style>
    <script>
        window.onload = function () {
            let form = document.getElementById("login-form")
            form.addEventListener('submit', function(event) {
                event.preventDefault(); // 阻止表单默认提交行为

                let formData = new FormData(form)
                let xhr = new XMLHttpRequest()
                xhr.open("POST", "/login", true)
                xhr.onreadystatechange = function () {
                    if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
                        let response = xhr.responseText
                        console.log("响应信息：", response)
                        let resultJson = JSON.parse(response)
                        // console.log("resultJson.flag：", resultJson.flag)
                        if (resultJson.flag === true) {
                            window.location.href = "http://localhost:8080/index"
                        } else {
                            alert("登录失败")
                        }
                    }
                }
                xhr.send(formData)
            })
        }
    </script>
</head>
<body>

<div class="container">
    <h2>Login</h2>
    <form id="login-form" action="http://localhost:8080/user/login">
        <label for="username">Username:</label>
        <input type="text" id="username" name="username" required>
        <label for="password">Password:</label>
        <input type="password" id="password" name="password" required>
        <input type="submit" value="Login">
    </form>
</div>

</body>
</html>
